package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Address;
import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface OrderDao_lzl {

    @Insert("insert into t_order values(#{id},#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    @SelectKey(statement = "select LAST_INSERT_ID()",keyProperty = "id",before = false,resultType = Integer.class)
    void addOrder(Order order);

    @Select("<script> select * from t_order"+
                "<where>"+
                    "<if test='memberId!=null'> and member_id=#{memberId} </if>"+
                    "<if test='orderDate!=null'> and orderDate=#{orderDate} </if>"+
                    "<if test='setmealId!=null'> and setmeal_id=#{setmealId} </if>"+
                "</where>"+
            "</script>"
    )
    List<Order> findOrderByMemberIdAndSetmealId(Order order);

    /*
     * 根据orderid，查询预约信息
     * */
    @Select("select m.name member,s.name setmeal,o.orderDate,o.orderType from t_order o,t_member m,t_setmeal s where o.member_id=m.id and o.setmeal_id=s.id and o.id=#{id}")
    Map findById(Integer id);

    /*
    *根据当前日期查询当前预约人数
    * */
    @Select("select count(*) from t_order where orderDate=#{value}")
    Integer findtodayOrderNumber(String reportDate);

    /*
    * 根据当前日期获取今日到诊数
    * */
    @Select("select count(*) from t_order where orderDate=#{value} and orderStatus='已到诊'")
    Integer findtodayVisitsNumber(String reportDate);

    /*
     * 獲取这周预约人数
     * */
    @Select("select count(*) from t_order where orderDate >= #{value}")
    Integer findOrderNumber(String date);

    /*
    * 获取这周的到诊数
    * */
    @Select("select count(*) from t_order where  orderStatus='已到诊' and orderDate >=#{value}")
    Integer findVisitsNumber(String date);

    /*
    * 获取热门套餐：名字，该套餐预约数，
    * */
    @Select("SELECT s.name name,COUNT(o.id) setmeal_count,COUNT(*)/(SELECT COUNT(*) FROM t_order) proportion " +
            "FROM t_order o,t_setmeal s WHERE s.id=o.setmeal_id  GROUP BY s.name ORDER BY setmeal_count DESC LIMIT 0,2")
    List<Map> getHotSetmeal();

    /*
     * 根据条件分页查询
     * */
    @Select("SELECT o.id id,DATE_FORMAT(o.orderDate,'%Y-%m-%d')  orderDate,o.orderType orderType,o.orderStatus orderStatus,s.name setmealName,s.id sid " +
            "FROM t_order o,t_setmeal s WHERE o.setmeal_id=s.id  AND  orderDate BETWEEN #{start} AND #{end} AND o.member_id=#{memberId}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "memberId",column = "member_id"),
            @Result(property = "orderDate",column = "orderDate"),
            @Result(property = "orderType",column = "orderType"),
            @Result(property = "orderStatus",column = "orderStatus"),
            @Result(property = "setmealId",column = "setmeal_id"),
            @Result(
                    property = "address",
                    column = "id",
                    javaType = Address.class,
                    one = @One(select = "com.itheima.dao.AddressDao.findByOrderId")
            )
    })
    Page<Map> findByCondition(Map map);
}
